/*
 * @FileName: fault.c
 * @Author: GreyQiu qiushaogui@aikosolar.com
 * @Date: 2023-07-18 10:14:16
 * @LastEditors: GreyQiu qiushaogui@aikosolar.com
 * @LastEditTime: 2023-08-09 15:27:04
 * @Description: 
 * 
 * Copyright (c) 2023, All Rights Reserved. 
 */

#include "fault.h"

/**
 * @description: 检查故障，更新故障状态
 * @param obj 故障单元地址
 * @return 无
 */
void fault_check(fault_t * obj)
{
    float value = *(obj->singal_addr);
    bool flag_tmp;

    if (obj->polarity == FAULT_CMP_POL_HIGH) {
        flag_tmp = (value > obj->threshold);
    } else {
        flag_tmp = (value <= obj->threshold);
    }

    if (obj->flag == false) { // 
        if (flag_tmp == false) {
            obj->time = 0;
        } else {
            obj->time++;
        }

        if (obj->time > obj->trig_time) { // 故障置位迟滞时间判断
            obj->flag = true;
            obj->time = 0;
        } 
    } else {
        if (obj->auto_clear == FAULT_AUTO_CLR_DISABLE) {
            return;
        }

        if (flag_tmp == true) {
            obj->time = 0;
        } else {
            obj->time++;
        }

        if (obj->time > obj->recovery_time) { // 故障清除迟滞时间判断
            obj->flag = false;
            obj->time = 0;
        } 
    }
}
